package leetCode;

import org.junit.Test;
import java.util.Hashtable;

/**
 * TODO
 *
 * @author 司松坡
 * @createTime 2023年03月24日
 */
public class LeetCode32 {
    public int longestValidParentheses(String s) {
        int max = 0;
        int length = s.length();
        int[] charArr = new int[length];
        for (int i = 1; i < length; i++) {
            if (s.charAt(i) == ')') {
                if (i - 1 > 0 && charArr[i - 1] == '(') {
                    if (i - 2 > 0) {
                        charArr[i] = charArr[i - 2] + 2;
                    } else {
                        charArr[i] = 2;
                    }
                } else {
                    if (i - charArr[i - 1] - 1 >= 0 && s.charAt(i - charArr[i - 1] - 1) == '(') {
                        if (i - charArr[i - 1] > 1) {
                            charArr[i] = charArr[i - 1] + charArr[i - charArr[i - 1] - 2];
                        } else {
                            charArr[i] = charArr[i - 1];
                        }
                    }
                }
            }
            max = Math.max(charArr[i],max);
        }
        return max;
    }

    @Test
    public void test(){
        System.out.println(longestValidParentheses("(()"));

    }
}
